"""
@Author：WangYuXiang
@E-mile：Hill@3io.cc
@CreateTime：2021/7/27 15:16
@DependencyLibrary：无
@MainFunction：无
@FileDoc： 
    handle_exception.py
    覆盖默认异常，呈现出更符合项目要求
    的错误提示。
@ChangeHistory:
    datetime action why
    example:
    2021/7/27 15:16 change 'Fix bug'
        
"""
from apps import app

from sanic.exceptions import NotFound,ServiceUnavailable,ServerError
from sanic.response import json

@app.exception(NotFound)
async def catch_anything(request, exception):
    
    debug = request.app.config.get('DEBUG')
    if debug:
        response_body = {
            'data': {'all_route':[i.path for i in app.router.routes_all.values() ]},
            'message': '未找到对应资源,请参考以上资源核对',
            'status': 0
        }
    else:
        response_body = {
            'data': {},
            'message': '未找到对应资源',
            'status': 0
        }
       
    return json(body=response_body, status=exception.status_code, )

@app.exception(ServiceUnavailable,ServerError)
async def catch_anything(request, exception):
    response_body = {
            'data': {},
            'message': '服务器内部发生错误',
            'status': 0
        }
    return json(body=response_body, status=exception.status_code, )